﻿using System;
using System.Collections.Generic;
using System.ServiceModel;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using OctoOS;
using OctoOS.Services;
using OctoOS.UI;

public partial class samples_discussion : OOSPage
{
	class PathInfo
	{
		[PathInfoMember(0, true)]
		internal int PageIndex = 0;

		// This will be called after the path info is parsed and this object is initialized
		internal void PostInit()
		{
			if(0 < PageIndex)
				PageIndex--;
		}
	}

	int pageIndex;

	protected void Page_Init(object sender, EventArgs e)
	{
		pageIndex = EnablePathInfo<PathInfo>().PageIndex;
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		AddBreadcrumb("", Urls.GetHome());
		AddBreadcrumb("Samples", Urls.Samples.GetHome());

		OOSRequest oosRequest = OOSRequest.Current;

		// In many cases GroupID could be the same as the ResourceID (unless you need multiple discussion groups associated with one resource)
		threadView.GroupID = oosRequest.ResourceID;

		// Since for this example we need just one discussion page, we use ResourceID as the ThreadID value. 
		// The most apps will use a unique ID of the entity you want to discuss instead of ResourceID.
		threadView.ThreadID = oosRequest.ResourceID;

		threadView.PageIndex = pageIndex;

		threadView.FirstPageUrl = Urls.Samples.GetDiscussion();
		threadView.PageUrlPattern = threadView.FirstPageUrl + "/{0}";	// {0} will be replaced with the page index
	}

	protected void SubmitPost(object sender, EventArgs e)
	{
		if(IsValid)
		{
			OOSRequest oosRequest = OOSRequest.Current;

			long postID;
			ICommentService srv = ServiceFactory.GetService<ICommentService>();
			using(srv as IDisposable)
			{
				postID = srv.AddPost(oosRequest.Token, oosRequest.ResourceID,
					threadView.GroupID, threadView.ThreadID, 0, postForm.Author, "", postForm.Message, TextFormat.Plain);
			}

			Response.Redirect(Urls.Samples.GetPostRedirectUrl(threadView.ThreadID, postID));
		}
	}
}